<template>
  <div class='login-container'>
    <div class="logo">
      <img src="./logo.png" alt="">
      <div class='logo-title'>
        <p class="logo-title-name">天尚设计集团分公司</p>
        <p style="font-size:12px;">TINSHINE DESIGN GROUP BRANCH</p>
      </div>
    </div>
    <el-form class='login' autoComplete='on' :model='loginForm' :rules='loginRules' ref='loginForm'>
      <div class="login-title">
        OA管理系统
      </div>
      <el-form-item prop='username'>
        <el-input name='username' type='text' v-model='loginForm.username' autoComplete='on' placeholder='请输入您的用户名'>
          <svg-icon slot="suffix" icon-class='users' class="icon"></svg-icon>
        </el-input>
      </el-form-item>
      <el-form-item prop='password'>
        <el-input name='password' :type='pwdType' v-model='loginForm.password' @keyup.enter.native='handleLogin'
                  autoComplete='on'
                  placeholder='请输入您的密码'>
          <svg-icon slot="suffix" icon-class='passwords' class="icon"></svg-icon>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" :loading='loading' @click.native.prevent='handleLogin' round>立即登录</el-button>
      </el-form-item>
      <!--<el-form-item>-->
      <!--<el-button type="text" class="form-text" size="small">忘记密码?</el-button>-->
      <!--|-->
      <!--<el-button type="text" class="form-text" size="small">立即注册</el-button>-->
      <!--</el-form-item>-->
    </el-form>
  </div>
</template>

<script>
  import { isvalidUsername } from '@/utils/validate'

  export default {
    name: 'login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [
            {
              required: true,
              trigger: 'blur',
              validator: validateUsername
            }
          ],
          password: [
            {
              required: true,
              trigger: 'blur',
              validator: validatePass
            }
          ]
        },
        loading: false,
        pwdType: 'password'
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store
              .dispatch('Login', this.loginForm)
              .then(() => {
                this.loading = false
                this.$router.push({
                  path: '/'
                })
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style rel='stylesheet/scss' lang='scss'>
  /* reset element-ui css */
  $bg: #fff;
  $grey: #8BA5B5;
  .login-container {
    .el-input {
      width: 250px;
      input {
        background: $grey;
        border: 0;
        color: #fff;
        border-radius: 20px;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #000 !important;
        }
      }
      .el-input__suffix {
        right: 15px;
      }
    }
    .el-form-item.is-required .el-form-item__label:before {
      content: '';
    }
  }
</style>

<style rel='stylesheet/scss' lang='scss' scoped>
  $grey: #8BA5B5;
  .login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    align-items: center;
    justify-content: center;
    background: url('./bj.png') no-repeat;
    background-size: 100% 100%;
    .logo {
      position: absolute;
      left: 10px;
      top: 10px;
      display: flex;
      flex-direction: row;
      img {
        width: 60px;
        height: 81px;
        margin: 25px;
      }
      .logo-title {
        color: #fff;
        margin: 30px 0;
        .logo-title-name {
          font-size: 20px;
          font-weight: bold;
          letter-spacing: 8px;
          -webkit-margin-before: 10px;
          -webkit-margin-after: 10px;
        }
      }
    }
    .login {
      padding: 30px;
      border: 1px solid $grey;
      background: rgba(139, 165, 181, 0.1);
      text-align: center;
      color: #fff;
      .login-title {
        margin-bottom: 15px;
        font-size: 20px;
      }
      .el-button.is-round {
        width: 100%;
      }
      .form-text {
        color: #fff;
        margin-right: 2px;
      }
    }
  }

</style>
